//=============================================================================
// TF Armory. Structure for buying weapons and equipment.
//=============================================================================

class TFArmory extends TFStation
	placeable;

state Ready
{
    function BeginState()
    {
        super.BeginState();

        tfgri(level.game.GameReplicationInfo).TeamCredits[myteam]+=20;

        if(myteam==0) setstaticmesh(StaticMesh'TFMedia.Buildings.tfArmoryRed');
        else setstaticmesh(StaticMesh'TFMedia.Buildings.tfArmoryBlu');

        settimer(1,true);
    }

    //regenerate ammo
    function Timer()
    {
        local float amount, amount2, save;   //amount of ammo that the weapons get
        local float ammo1, maxammo1, ammo2, maxammo2;  //current and max ammo's
        local int count;
        local inventory inv;    //players inventory
        local tfweapon w;       //players weapon
        local bool bplaysound;
        local xemitter gfx;

        FindNear();  //find pawn near armory

        if(grade==3)        //if armory is level 3
            tfgri(level.Game.GameReplicationInfo).AddTeamSkill(1, myteam);  //add 0.1 skill to all team mates per tick

        if(near==none) return;   //if no pawn is found, do nothing

        if(vsize(location-near.location)>192 || near.Controller==none || near.PlayerReplicationInfo.Team.TeamIndex!=myteam)  //if pawn is not within 192m or pawn has no controller or pawn is not same team as armory
        {
             near=none;    //set near to none
             return;       // and do nothing
        }

        if(tfpri(near.PlayerReplicationInfo).spawnprotected)             //if the pawns spawn protection is on
            tfpri(near.PlayerReplicationInfo).spawnprotected=false;      // turn it off

        bplaysound=false;

        if(grade==3)
        {
            tfpri(near.PlayerReplicationInfo).AddSkill(10,0,true);     //if armory level is 3, add 10 skill per tick to pawn
            tfgri(level.Game.GameReplicationInfo).AddTeamSkill(1, myteam,true);
        }

	    for( Inv=near.Inventory; Inv!=None && count<1000; Inv=Inv.Inventory )   //pawns inventory = inv, inventory is not none and count is less then 1000 inv = the current inventory
	    {
		    Count++;     // used to limit the loop if it runs for too long
		    if(tfweapon(inv)!=none && tfweapon(inv).bDoAmmoRegen)  // checks if the pawn has a weapon and if the weapon allows ammo regen
		    {
		        w=tfweapon(inv);     // w = the weapon

		        ammo1=w.AmmoAmount(0);    //ammo1 = the weapons current primary fire ammo
		        maxammo1=w.MaxAmmo(0);    //maxammo1 = the weapons max primary fire ammo
		        ammo2=w.AmmoAmount(1);    //ammo1 = the weapons current secondary fire ammo
		        maxammo2=w.MaxAmmo(1);    //maxammo2 = the weapons max secondary fire ammo
                //this just gets the amount to give the weapon
                if((ammo1<maxammo1 || (maxammo2>0 && ammo2<maxammo2)) && !w.GetFireMode(0).bisfiring && !w.GetFireMode(1).bisfiring) //only regen if current ammo is less then max ammo, and for secondary only if secondary has a ammo type. and as long as the weapon is not firing
                    bplaysound=true;   // if it meets these req set bplaysound to true

                if(bplaysound)  // if this is true, start regenerating
                {
		            if(maxammo1>0)   // if primary fire has a ammo type
		            {
                        if(grade==1)
                            amount=w.default.AmmoRegenAmount[0];
                        else if(grade==2)
                            amount=w.default.AmmoRegenAmount[0]*2;
                        else if(grade==3)
                            amount=w.default.AmmoRegenAmount[0]*3;
                      //if amount is less then 1 this will quick fix it because addammo function only accepts integers
                    save=int(amount);                 //turns float values into integer values
		            amount-=save;                    // frand will randomly get a float between 0 and 1
                    if(frand()<amount) amount=1;    // if frand is less then amount then amount = 1 and you get 1 ammo
                    amount+=save;
                    }

		            if(maxammo2>0)  // if secondary fire has a ammo type
		            {    //you get base amount multiplied by armory level
                        if(grade==1)
                            amount2=w.default.AmmoRegenAmount[1];
                        else if(grade==2)
                            amount2=w.default.AmmoRegenAmount[1]*1.5;
                        else if(grade==3)
                            amount2=w.default.AmmoRegenAmount[1]*2;
                      //if amount2 is less then 1 this will quick fix it because addammo function only accepts integers
                    save=int(amount2);                   //turns float values into integer values
                    amount2-=save;                      // frand will randomly get a float between 0 and 1
                    if(frand()<amount2) amount2=1;     // if frand is less then amount2 then amount2 = 1 and you get 1 ammo
                    amount2+=save;
                    }
                    //this gives the weapons ammo
		            if(amount+amount2>=1) //make sure the weapon is actually going to get some ammo
                    {
                        if(maxammo1>0) w.addammo(amount,0);  //if primary fire has a ammo type, add amount to current ammo of primary ammo
                        if(maxammo2>0) w.addammo(amount2,1); //if secondary fire has a ammo type, add amount to current ammo of secondary ammo
                    }
                }
            }
        }
        if(bplaysound)
        {
         PlaySound(sound'TFMedia.TFRepAmmo',SLOT_None,0.3);   // if bplaysound is true then play this sound
         gfx=spawn(class'Taskforces.tfArmoryFX',,,near.Location);    //spawn the line between pawn and armory
         gfx.mSpawnVecA=Location;
        }
    }
}

function Destructed(pawn killer, bool recycled)
{
    tfgri(level.game.GameReplicationInfo).TeamCredits[myteam]-=10;

    super.Destructed(killer, recycled);
}

defaultproperties
{
     BuildTime=60.000000
     MaxHealth=1000.000000
     chunkcount=5
     BuildingName="Armory"
     NameIndex=4
     teamflareDS=0.900000
     StaticMesh=StaticMesh'TFMedia.Buildings.tfArmory'
     AmbientSound=Sound'TFMedia.Buildings.tfArmoryAmb'
     bUseCylinderCollision=True
     CollisionRadius=40.000000
     CollisionHeight=56.000000
}
